#include "OGLRenderBuffer.h"

#include <assert.h>

namespace Hope {

    OGLRenderBuffer::OGLRenderBuffer(GLenum target, GLenum usage, uint32_t size) : m_target(target), m_usage(usage), m_size(size)
    {

    }

    OGLRenderBuffer::~OGLRenderBuffer()
    {
        DeleteBuffer();
    }

    void OGLRenderBuffer::SetData(const void * data)
    {
        assert(0 == m_vbo);

        glGenBuffers(1, &m_vbo);
        glBindBuffer(m_target, m_vbo);
        glBufferData(m_target, m_size, data, m_usage);
        glBindBuffer(m_target, 0);
    }

    void OGLRenderBuffer::DeleteBuffer()
    {
        if (m_vbo != 0)
        {
            glDeleteBuffers(1, &m_vbo);
        }
        m_vbo = 0;
    }

    void OGLRenderBuffer::Active()
    {
        glBindBuffer(m_target, m_vbo);
    }

    void OGLRenderBuffer::UpdateBuffer(uint32_t offset, uint32_t size, const void * data)
    {
        glBindBuffer(m_target, m_vbo);
        glBufferSubData(m_target, offset, size, data);
        glBindBuffer(m_target, 0);
    }

}